#include <cstring>

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int newestPos[128];
        memset(newestPos, -1, sizeof(newestPos));
        
        int lastSubstrEndPos = -1;
        int res = 0;
        
        for (int curPos=0; curPos<s.size(); curPos++)
        {
            char curChar = s[curPos];
            if (newestPos[curChar] > lastSubstrEndPos)
            {
                lastSubstrEndPos = newestPos[curChar];
            }
            newestPos[curChar] = curPos;
            
            res = max(res, curPos - lastSubstrEndPos);
        }
        
        return res;
    }
};